<template>
  <div class="stock-unlock-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>库存解锁</span>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="物料编码">
          <el-input v-model="queryForm.materialCode" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="锁定日期">
          <el-date-picker
            v-model="queryForm.lockDate"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="materialCode" label="物料编码" />
        <el-table-column prop="materialName" label="物料名称" />
        <el-table-column prop="quantity" label="锁定数量" />
        <el-table-column prop="reason" label="锁定原因" />
        <el-table-column prop="operator" label="锁定人" />
        <el-table-column prop="lockTime" label="锁定时间" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button link type="primary" @click="handleUnlock(scope.row)">解锁</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="operation-buttons">
        <el-button type="primary" @click="handleBatchUnlock">批量解锁</el-button>
      </div>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 解锁确认对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="库存解锁"
      width="500px"
    >
      <el-form :model="unlockForm" label-width="100px">
        <el-form-item label="解锁原因" required>
          <el-input 
            v-model="unlockForm.reason"
            type="textarea"
            placeholder="请输入解锁原因"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleUnlockConfirm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const queryForm = reactive({
  materialCode: '',
  lockDate: ''
})

const tableData = ref([
  {
    materialCode: 'M001',
    materialName: '物料A',
    quantity: 100,
    reason: '质量检查',
    operator: '张三',
    lockTime: '2024-03-20 10:00:00'
  }
])

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

const dialogVisible = ref(false)
const unlockForm = reactive({
  reason: '',
  selectedRows: []
})

const handleSearch = () => {
  // 实现查询逻辑
}

const handleReset = () => {
  queryForm.materialCode = ''
  queryForm.lockDate = ''
}

const handleUnlock = (row) => {
  dialogVisible.value = true
  unlockForm.selectedRows = [row]
}

const handleBatchUnlock = () => {
  const selection = [] // 获取表格选中行
  if (selection.length === 0) {
    ElMessage.warning('请选择要解锁的记录')
    return
  }
  dialogVisible.value = true
  unlockForm.selectedRows = selection
}

const handleUnlockConfirm = () => {
  if (!unlockForm.reason) {
    ElMessage.warning('请输入解锁原因')
    return
  }
  // 实现解锁逻辑
  ElMessage.success('解锁成功')
  dialogVisible.value = false
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.stock-unlock-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.operation-buttons {
  margin: 20px 0;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 